from PySide6.QtCore import Qt, QTranslator, QSize
from PySide6.QtGui import QGuiApplication, QIcon
from PySide6.QtWidgets import QApplication
from qfluentwidgets import (
    FluentTranslator,
    SplashScreen,
    FluentIcon as FIF,
    FluentWindow,
    NavigationItemPosition,
    setTheme,
    Theme,
)

from app.common.config import cfg
from app.common.translator import Translator
from app.utils.log_rec import logger
from .interfaces import InterfacesView
from ..common.signal_bus import signalBus
from ..resources import resources_rc

from app.view.interfaceSettings.settingInterface import SettingInterface
from app.view.emulatorLaunchTool.interfaceEmulatorLaunchTool import (
    InterfaceEmulatorLaunchTool,
)


class MainWindow(FluentWindow):
    def __init__(self):
        super().__init__()
        self.t = Translator()
        self.init_ui()

        # 初始化接口
        logger.debug("初始化菜单")
        self.interface_settings = SettingInterface(self)
        self.interface_emulator_launch_tool = InterfaceEmulatorLaunchTool(self)

        # 启用亚克力效果
        logger.debug("启用亚克力效果")
        self.navigationInterface.setAcrylicEnabled(True)

        self.__init_signal_to_slot()

        self.init_navigation()
        self.splash_screen.finish()

    def init_ui(self):
        """

        :return:
        """
        self.resize(
            cfg.get(cfg.main_highlighter) if cfg.get(cfg.main_highlighter) else 960,
            cfg.get(cfg.main_wide) if cfg.get(cfg.main_wide) else 780,
        )
        self.setMinimumWidth(760)
        self.setWindowIcon(QIcon(":/app/images/tools.png"))
        self.setWindowTitle(self.t.title)

        # self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))

        # 创建初始屏幕
        logger.debug("创建初始屏幕")
        self.splash_screen = SplashScreen(self.windowIcon(), self)
        self.splash_screen.setIconSize(QSize(106, 106))
        self.splash_screen.raise_()

        desktop = QGuiApplication.primaryScreen().geometry()
        _, h = desktop.width(), desktop.height()
        self.move(_ // 2 - self.width() // 2, h // 2 - self.height() // 2)
        self.show()
        QApplication.processEvents()

    def __init_signal_to_slot(self):
        signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)
        signalBus.switchToSampleCard.connect(self.switch_to_sample)
        # signalBus.supportSignal.connect(self.onSupport)

    def init_navigation(self):
        """

        :return:
        """
        pos = NavigationItemPosition.SCROLL
        self.navigationInterface.addSeparator()
        self.addSubInterface(
            self.interface_emulator_launch_tool,
            FIF.ERASE_TOOL,
            self.t.InterfaceEmulatorLaunchTool,
            pos,
        )
        self.addSubInterface(
            self.interface_settings,
            FIF.SETTING,
            self.t.settings,
            NavigationItemPosition.BOTTOM,
        )

    def resizeEvent(self, e):
        super().resizeEvent(e)
        if hasattr(self, "splash_screen"):
            self.splash_screen.resize(self.size())

    def switch_to_sample(self, router_key, index):
        """

        :param router_key:
        :param index:
        :return:
        """
        interfaces = self.findChildren(InterfacesView)
        for w in interfaces:
            if w.objectName() == router_key:
                self.stackedWidget.setCurrentWidget(w, False)
                w.scroll_to_card(index)
